<template>
  <a-card :bordered="false">
    <div class="card-title">{{ $route.query.course }}-{{ $route.meta.title }}</div>
    <div class="table-operator">
      <a-row type="flex" justify="space-between">
        <a-col :span="6">
          <a-button v-action:add type="primary" icon="plus" @click="handleAdd">新增</a-button>
        </a-col>
        <a-col :span="2">
          <a-button v-action:students class="student-btn" icon="unordered-list" @click="showStudents">学员列表</a-button>
          <student-list :visible="studentListVisible" @close="onStudentListClose" :students="studentList" />
        </a-col>
      </a-row>
    </div>
    <s-table ref="table" rowKey="lesson_id" :loading="isLoading" :columns="columns" :data="loadData" :pageSize="15">
      <div slot="name" slot-scope="text, item">
        <span class="course-name">{{ text }}</span>
      </div>
      <div slot="lessons" slot-scope="text, item">{{ text }}节课</div>
      <div slot="class_hour" slot-scope="text, item">{{ text }}分钟</div>
      <div slot="opening_date" slot-scope="text, item">
        <a-tooltip placement="right" arrow-point-at-center>
          <div slot="title">
            {{ text }}
          </div>
          <div class="opening-date">{{ text }}</div>
        </a-tooltip>
      </div>
      <div slot="course_detail" slot-scope="text, item">
        <a-tooltip placement="right" arrow-point-at-center>
          <div slot="title">
            {{ text }}
          </div>
          <div class="course-detail">{{ text }}</div>
        </a-tooltip>
      </div>
      <div slot="status" slot-scope="text, item">
        {{ statusText(text) }}
      </div>
      <div slot="recommend" slot-scope="text, item">
        {{ recommendText(text) }}
      </div>
      <div class="actions" slot="action" slot-scope="text, item">
        <a v-action:edit @click="handleEdit(item)">编辑</a>
        <template>
          <a v-action:delete @click="handleDelete(item)">删除</a>
        </template>
      </div>
    </s-table>
    <AddForm ref="AddForm" @handleSubmit="handleRefresh" />
    <EditForm ref="EditForm" @handleSubmit="handleRefresh" />
  </a-card>
</template>

<script>
import * as Api from '@/api/course/lesson'
import * as CourseApi from '@/api/course/index'
import { STable } from '@/components'
import AddForm from './lesson/AddForm.vue'
import EditForm from './lesson/EditForm.vue'
import StudentList from './detail/StudentList.vue'

export default {
  name: 'CourseDetail',
  components: {
    STable,
    AddForm,
    EditForm,
    StudentList
  },
  data() {
    return {
      // 查询参数
      queryParam: {},
      // 正在加载
      isLoading: false,
      // 表头
      columns: [
        {
          title: '课时ID',
          dataIndex: 'lesson_id',
          align: 'center',
        },
        {
          title: '课时名称',
          dataIndex: 'name',
          scopedSlots: { customRender: 'name' },
          align: 'center',
        },
        {
          title: '上课日期',
          dataIndex: 'class_date',
          align: 'center',
        },
        {
          title: '上课时间',
          dataIndex: 'class_time',
          align: 'center',
        },
        {
          title: '上课地点',
          dataIndex: 'class_place',
          align: 'center',
        },
        {
          title: '排序',
          dataIndex: 'sort',
          align: 'center',
        },
        {
          title: '添加时间',
          dataIndex: 'create_time',
          align: 'center',
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: '180px',
          scopedSlots: { customRender: 'action' },
          align: 'center',
        },
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: (param) => {
        return Api.list({ ...param, ...this.queryParam, courseId: this.$route.query.courseId }).then((response) => {
          return response.data
        })
      },
      studentListVisible: false,
      studentList: []
    }
  },
  computed: {
    statusText() {
      const textArr = ['下架', '上架']
      return (text) => {
        return textArr[text]
      }
    },

    recommendText() {
      const textArr = ['否', '是']
      return (text) => {
        return textArr[text]
      }
    },
  },
  created() {},
  methods: {
    /**
     * 新增记录
     */
    handleAdd() {
      this.$refs.AddForm.add()
    },

    /**
     * 编辑记录
     */
    handleEdit(item) {
      this.$refs.EditForm.edit(item)
    },

    /**
     * 删除记录
     */
    handleDelete(item) {
      const app = this
      const modal = this.$confirm({
        title: '您确定要删除该记录吗?',
        content: '删除后不可恢复',
        onOk() {
          return Api.del({ lessonId: item.lesson_id })
            .then((result) => {
              app.$message.success(result.message, 1.5)
              app.handleRefresh()
            })
            .finally((result) => {
              modal.destroy()
            })
        },
      })
    },

    /**
     * 刷新列表
     * @param Boolean bool 强制刷新到第一页
     */
    handleRefresh(bool = false) {
      this.$refs.table.refresh(bool)
    },

    showStudents() {
      const courseId = this.$route.query.courseId
      CourseApi.studentList({courseId}).then(res => {
        console.log(res)
        this.studentList = res.data
      })
      this.studentListVisible = true
    },

    onStudentListClose() {
      this.studentListVisible = false
    }
  },
}
</script>

<style lang="less" scoped>
// .course-name {
//   cursor: pointer;
//   font-weight: 700;
//   color: #1890ff;
// }
.opening-date,
.course-detail {
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.student-btn {
  width: 100%;
}
</style>
